/**
 * fileName: CS_YLES_Import_item_barcode_CSV
 * @NApiVersion 2.1
 * @NScriptType ClientScript 
 */
define(["require", "exports", "N/search", "N/url", "N/currentRecord", "../tools/YLES_CLIENT_TOOL", "../tools/YLES_RELATED_RECORDTYPE"], function (require, exports, search, url, crd, YLES_CLIENT_TOOL_1, YLES_RELATED_RECORDTYPE_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.historyImport = exports.refresh = exports.exportCSV = exports.updateCSV = exports.validateCSV = exports.saveRecord = exports.pageInit = void 0;
    const slid = 'info_list';
    function pageInit(cxt) {
        const rec = cxt.currentRecord;
        //判断是否启动校验进度条
        if (jQuery('#check_csv_progress').length > 0) {
            try {
                (0, YLES_CLIENT_TOOL_1.initCheckCSVProgress)();
            }
            catch (e) {
                console.log('启动进度条失败', e);
            }
        }
        // jQuery("#progress_fs").hide();
        // var runtime = require("N/runtime");
        // var crd = require("N/currentRecord");
    }
    exports.pageInit = pageInit;
    //saveRecord
    function saveRecord(cxt) {
        const rec = cxt.currentRecord;
        //校验程序进程
        let ckSts = checkImportProcess(rec);
        if (!ckSts)
            return false;
        return true;
    }
    exports.saveRecord = saveRecord;
    /**
     * 校验CSV
     */
    function validateCSV() {
        const cur_rec = crd.get();
        const filename = cur_rec.getValue({ fieldId: 'upload_file' });
        console.log('filename', filename);
        if (/.*\.csv/.test(filename)) {
            cur_rec.setValue({ fieldId: 'submit_operate', value: 'validate' });
            (0, YLES_CLIENT_TOOL_1.addScreenDisabled)();
            clickSubmit();
        }
        else {
            alert('Please upload csv file.');
            cur_rec.setValue({ fieldId: 'upload_file', value: '' });
        }
    }
    exports.validateCSV = validateCSV;
    function clickSubmit() {
        jQuery('#submitter').click();
    }
    //提交执行更新逻辑
    function updateCSV() {
        const cur_rec = crd.get();
        cur_rec.setValue({ fieldId: 'submit_operate', value: 'import' });
        clickSubmit();
    }
    exports.updateCSV = updateCSV;
    //执行下载
    function exportCSV() {
        return (0, YLES_CLIENT_TOOL_1.exportMyCSV)();
    }
    exports.exportCSV = exportCSV;
    //校验导入程序执行状态
    function checkImportProcess(rec) {
        let rtn = true;
        const Search = search.create({
            type: (0, YLES_RELATED_RECORDTYPE_1.CustomRecordType)().IMPORT_ITEM_BARCODE_LOG,
            filters: [
                ['custrecord_yles_status', 'anyof', ['3']], 'and',
                ['custrecord_yles_operate', 'doesnotcontain', 'validate'], 'and',
                ['custrecord_yles_operate', 'isnotempty', ''], 'and',
                ['isinactive', 'anyof', false]
            ],
            columns: [
                { name: 'internalid' },
                { name: 'internalid' },
                { name: 'custrecord_yles_user' },
                { name: 'created' },
            ]
        });
        Search.run().each(res => {
            rtn = false;
            alert(`Warning!\n 
            There are plans to generate， \n
            the operator is ${res.getText(Search.columns[2])} \n 
            time is ${res.getValue(Search.columns[3])} .`);
            return false;
        });
        return rtn;
    }
    //刷新界面
    function refresh() {
        window.onbeforeunload = null;
        window.location.reload();
    }
    exports.refresh = refresh;
    function historyImport() {
        const targetUrl = url.resolveScript({
            scriptId: 'customscript_yles_import_csv_history_sl',
            deploymentId: 'customdeploy_yles_import_csv_history_sl'
        });
        window.open(targetUrl);
    }
    exports.historyImport = historyImport;
});
